package cn.edu.zjgsu.ito.service;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class AdminSingleStuScoreService {

	/**
	 * 学生成绩单---打印
	 * 杨昆鹏
	 * */

	public List<Record> stuGrade(String passportNo){

		List<Record> stuGrade = new ArrayList<Record>();
		List<Record> stuGradeInf = Db.find("select * from student_grade,test where student_grade.testNo=test.testNo and passportNo=?",passportNo);


		for(int i = 0; i < stuGradeInf.size() ; i++){
			String testLock = stuGradeInf.get(i).getStr("testLock");
			String testType = stuGradeInf.get(i).getStr("testType");
			if(testLock != null){
				if(testLock.equals("1")){


					System.out.println("==========");

					String termValue1 = stuGradeInf.get(i).getStr("termValue");
					String courseNo = stuGradeInf.get(i).getStr("courseNo");
					String courseName = stuGradeInf.get(i).getStr("courseName");
					String grade = stuGradeInf.get(i).getStr("grade");
					int gradeStatus = stuGradeInf.get(i).getInt("gradeStatus");
					//学生个人信息
					Record stuInf = Db.findFirst("select * from student_info where passportNo=?",passportNo);
					String sNo = stuInf.getStr("sNo");
					String firstName = stuInf.getStr("firstName");
					String lastName = stuInf.getStr("lastName");
					String dateyearstar = stuInf.getStr("eduStartDate");

					int dateyearlast = Integer.parseInt(dateyearstar) + 4;
					String eduStartDate = dateyearstar + "-" + dateyearlast;
					//课程学分
					Record credites = Db.findFirst("select * from course where courseNo=?",courseNo);
					int credit = credites.getInt("credit");

					//对学期进行处理
					String termValue = "";
					String temp1 = termValue1.substring(0,4);
					String temp2 = termValue1.substring(5,9);
					String temp3 = termValue1.substring(10,11);
					if(temp3.equals("1")){
						termValue = temp1 + ".9" +"-"+ temp2 + ".1" ;
					}if(temp3.equals("2")){
						termValue = temp2 + ".3" +"-"+ temp2 + ".7" ;
					}

					//对最终成绩进行处理
					String gradeLevel = "";
					if(grade.equals("优秀")||grade.equals("良好")||grade.equals("中等")||
							grade.equals("及格")||grade.equals("不及格")){
						switch(grade){
						case "优秀":
							gradeLevel = "A";
							break;
						case "良好":
							gradeLevel = "B";
							break;
						case "中等":
							gradeLevel = "C";
							break;
						case "及格":
							gradeLevel = "P";
							break;
						case "不及格":
							gradeLevel = "F";
							break;
						}
					}else if(stuGradeInf.get(i).getStr("testType").equals("2") && (grade.equals("旷考")||grade.equals("作弊"))){
						gradeLevel = "F";
					}else{
						int grades = Integer.parseInt(grade);
						System.out.println(grades);
						if(grades >= 0 && grades <= 100){
							if(grades >= 90){
								gradeLevel = "A";
							}else if(grades >= 80 ){
								gradeLevel = "B";
							}else if(grades >= 70){
								gradeLevel = "C";
							}else if(grades >= 60){
								gradeLevel = "P";
							}else{
								gradeLevel = "F";
							}
						}
					}
					System.out.println(termValue+courseNo+courseName+credit+gradeStatus+gradeLevel+sNo+grade+firstName+lastName+eduStartDate);
					Record stu = new Record().set("termValue",termValue).set("courseNo", courseNo).set("courseName",courseName)
							.set("credit",credit).set("gradeStatus",gradeStatus).set("grade",grade).set("gradeLevel",gradeLevel)
							.set("sNo",sNo).set("firstName",firstName).set("lastName",lastName).set("eduStartDate",eduStartDate)
							.set("testType",stuGradeInf.get(i).getStr("testType"));
					System.out.println("=====1====");
					stuGrade.add(stu);
				}
			}else if(testType.equals("3")){
				System.out.println("这是历史成绩");
				//对学期进行处理
				String termValue1 = stuGradeInf.get(i).getStr("termValue");
				String termValue = "";
				String temp1 = termValue1.substring(0,4);
				String temp2 = termValue1.substring(5,9);
				String temp3 = termValue1.substring(10,11);
				if(temp3.equals("1")){
					termValue = temp1 + ".9" +"-"+ temp2 + ".1" ;
				}if(temp3.equals("2")){
					termValue = temp2 + ".3" +"-"+ temp2 + ".7" ;
				}
				//个人信息
				Record stuInf = Db.findFirst("select * from student_info where passportNo=?",passportNo);
				String sNo = stuInf.getStr("sNo");
				//对成绩进行处理
				String grade = stuGradeInf.get(i).getStr("grade");
				String gradeLevel = "";
				if(grade.equals("优秀")||grade.equals("良好")||grade.equals("中等")||
						grade.equals("及格")||grade.equals("不及格")){
					switch(grade){
					case "优秀":
						gradeLevel = "A";
						break;
					case "良好":
						gradeLevel = "B";
						break;
					case "中等":
						gradeLevel = "C";
						break;
					case "及格":
						gradeLevel = "P";
						break;
					case "不及格":
						gradeLevel = "F";
						break;
					}
				}else if(grade.equals("旷考")||grade.equals("作弊")){
					gradeLevel = "F";
				}else{
					int grades = Integer.parseInt(grade);
					System.out.println(grades);
					if(grades >= 0 && grades <= 100){
						if(grades >= 90){
							gradeLevel = "A";
						}else if(grades >= 80 ){
							gradeLevel = "B";
						}else if(grades >= 70){
							gradeLevel = "C";
						}else if(grades >= 60){
							gradeLevel = "P";
						}else{
							gradeLevel = "F";
						}
					}
				}
				
				String courseNo = stuGradeInf.get(i).getStr("courseNo");
				Record courseInf = Db.findFirst("select * from course where courseNo=?",courseNo);
				String courseName = courseInf.getStr("courseName");
				String credit = (courseInf.getInt("credit")).toString();
				Record stu = new Record().set("termValue",termValue).set("courseNo", courseNo).set("courseName",courseName)
						.set("credit",credit).set("grade",grade).set("gradeLevel",gradeLevel)
						.set("sNo",sNo).set("testType",stuGradeInf.get(i).getStr("testType"));
				stuGrade.add(stu);
			}
		}
		return stuGrade;
	}

	//对传前台的成绩进行处理
	public List<List<Record>> dataPrint(String passportNo){
		List<Record> allStuTerm = Db.find("select distinct termValue from student_grade where passportNo=?",passportNo);//多少学期
		List<List<Record>> allStuGra = new ArrayList<List<Record>>();
		System.out.println(allStuTerm.size()+"9999999999999999999");

		//return
		List<List<Record>> reAllStuGra = new ArrayList<List<Record>>();

		for(int i = 0;i < allStuTerm.size();i++){
			String term = allStuTerm.get(i).getStr("termValue");//拿到的所有学期
			List<Record> oneTerm = Db.find("select * from student_grade where termValue=? and passportNo=?",term,passportNo);
			allStuGra.add(oneTerm);
		}

		for(int i = 0;i < allStuGra.size();i++){
			List<Record> reOneTerm = new ArrayList<Record>();
			for(int j = 0;j < allStuGra.get(i).size();j++){
				String termValue1 = allStuGra.get(i).get(j).getStr("termValue");
				String courseNo = allStuGra.get(i).get(j).getStr("courseNo");
				String courseName = allStuGra.get(i).get(j).getStr("courseName");
				String grade = allStuGra.get(i).get(j).getStr("grade");
				int gradeStatus = allStuGra.get(i).get(j).getInt("gradeStatus");
				//学生个人信息
				Record stuInf = Db.findFirst("select * from student_info where passportNo=?",passportNo);
				String sNo = stuInf.getStr("sNo");
				String firstName = stuInf.getStr("firstName");
				String lastName = stuInf.getStr("lastName");
				String dateyearstar = stuInf.getStr("eduStartDate");

				int dateyearlast = Integer.parseInt(dateyearstar) + 4;
				String eduStartDate = dateyearstar + "-" + dateyearlast;
				//课程学分
				Record credites = Db.findFirst("select * from course where courseNo=?",courseNo);
				int credit = credites.getInt("credit");

				//对学期进行处理
				String termValue = "";
				String temp1 = termValue1.substring(0,4);
				String temp2 = termValue1.substring(5,9);
				String temp3 = termValue1.substring(10,11);
				if(temp3.equals("1")){
					termValue = temp1 + ".9" +"-"+ temp2 + ".1" ;
				}if(temp3.equals("2")){
					termValue = temp2 + ".3" +"-"+ temp2 + ".7" ;
				}

				//对最终成绩进行处理
				String gradeLevel = "";
				if(grade.equals("优秀")||grade.equals("良好")||grade.equals("中等")||
						grade.equals("及格")||grade.equals("不及格")){
					switch(grade){
					case "优秀":
						gradeLevel = "A";
						break;
					case "良好":
						gradeLevel = "B";
						break;
					case "中等":
						gradeLevel = "C";
						break;
					case "及格":
						gradeLevel = "P";
						break;
					case "不及格":
						gradeLevel = "F";
						break;
					}
				}else{
					int grades = Integer.parseInt(grade);
					System.out.println(grades);
					if(grades >= 0 && grades <= 100){
						if(grades >= 90){
							gradeLevel = "A";
						}else if(grades >= 80 ){
							gradeLevel = "B";
						}else if(grades >= 70){
							gradeLevel = "C";
						}else if(grades >= 60){
							gradeLevel = "P";
						}else{
							gradeLevel = "F";
						}
					}
				}
				System.out.println(termValue+courseNo+courseName+credit+gradeStatus+gradeLevel+sNo+grade+firstName+lastName+eduStartDate);
				Record stu = new Record().set("termValue",termValue).set("courseNo", courseNo).set("courseName",courseName)
						.set("credit",credit).set("gradeStatus",gradeStatus).set("grade",grade).set("gradeLevel",gradeLevel)
						.set("sNo",sNo).set("firstName",firstName).set("lastName",lastName).set("eduStartDate",eduStartDate);
				System.out.println("=====1====");
				reOneTerm.add(stu);
			}
			reAllStuGra.add(reOneTerm);
		}
		System.out.println("666666666666666666666666666");
		for(int i = 0;i < reAllStuGra.size();i++){
			for(int j = 0;j < reAllStuGra.get(i).size();j++){
				System.out.println(reAllStuGra.get(i).get(j).getStr("termValue"));
			}
			System.out.println("------------");
		}
		return reAllStuGra;
	}

}
